/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.yami.shop.bean.app.dto.ResourcesInfoDto;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.service.AttachFileService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传 controller
 * @author lgh
 *
 */

@RestController
@RequestMapping("/p/file")
@AllArgsConstructor
public class FileController {

	private AttachFileService attachFileService;

	private ShopConfig shopConfig;

	@PostMapping("/upload")
    @ApiOperation(value = "文件上传接口", notes = "上传文件，返回文件路径与域名")
	public ResponseEntity<ResourcesInfoDto> uploadFile(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
            return ResponseEntity.noContent().build();
        }

		String fileName = attachFileService.uploadFile(file.getBytes(),file.getOriginalFilename());

		String resourcesUrl = shopConfig.getDomain().getResourcesDomainName() + "/";

		ResourcesInfoDto resourcesInfoDto = new ResourcesInfoDto();
		resourcesInfoDto.setResourcesUrl(resourcesUrl);
		resourcesInfoDto.setFilePath(fileName);
		return ResponseEntity.ok(resourcesInfoDto);
	}

	@PostMapping("/uploadImFile")
	@ApiOperation(value = "聊天文件上传接口", notes = "上传文件，返回文件路径与域名")
	public ResponseEntity<ResourcesInfoDto> uploadImFile(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
			return ResponseEntity.noContent().build();
		}

		String fileName = attachFileService.uploadImFile(file.getBytes(),file.getOriginalFilename());

		String resourcesUrl = shopConfig.getDomain().getResourcesDomainName() + "/";

		ResourcesInfoDto resourcesInfoDto = new ResourcesInfoDto();
		resourcesInfoDto.setResourcesUrl(resourcesUrl);
		resourcesInfoDto.setFilePath(fileName);
		return ResponseEntity.ok(resourcesInfoDto);
	}
}
